Option Strict Off
Option Explicit On
Module ShutterMacro
	'This module reads a macro text file and sends keyboard commands to another program that is running. The macro filename is held in
	'the MacroFilename variable. The module first opens the macro file to obtain the target program name, then reopens the file to run through
	'each line to complete the commands.
	
	
	'API call to send a keyboard action do not be tempted to use Sendkeys as it is buggy and triggers other programs etc.
	Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
	Const KEYEVENTF_EXTENDEDKEY As Integer = &H1
	Const KEYEVENTF_KEYUP As Integer = &H2
	
	'API for shutter macro to enable the program to focus on other programs
	Private Declare Function FindWindow Lib "user32.dll"  Alias "FindWindowA"(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer 'finds window handle of window name supplied in string
	Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Integer) As Integer 'Sets the currently active window
	Public Sub TriggerCameraShutter()
		Dim TargetProgram As String 'The name of the target program (Usually the title in the Programs Window)
		Dim TargetHandle As Integer 'The handle to the window
		Dim DataType2 As String
		Dim KeyAscii As Integer
		Dim TimeToWait As Double 'The time for the macro to pause
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(MacroFilename) <> "" Then 'The macro file  exist so continue
			FileOpen(13, MacroFilename, OpenMode.Input)
			Do Until (EOF(13) = True)
				Input(13, DataType2)
				If UCase(DataType2) = "TARGET" Then 'Look to see if the line begins with TARGET
					Input(13, TargetProgram) 'Load in the target program name eg " EOS 1000D" "Untitled - Notepad" etc.
					Debug.Print(TargetProgram)
					GoTo FoundTargetProg
				End If
			Loop 
			Call MsgBox("The Camera Macro file does not contain a TARGET line to tell me " & Chr(13) & "which program to send commands to", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			FileClose(13)
			GoTo LeaveEarly 'The macro file is invalid, so exit the routine
FoundTargetProg: 
			FileClose(13)
			Debug.Print("Camera Shutter Triggered")
			TargetHandle = FindWindow(vbNullString, TargetProgram) 'Find the target window and return its handle
			If (TargetHandle <> 0) Then 'Check if the target program exists (ie the handle is found and is not zero)
				TargetHandle = SetForegroundWindow(TargetHandle) 'Focus on the target program
				
				FileOpen(13, MacroFilename, OpenMode.Input) 'Reopen the macro file
				Do Until (EOF(13) = True)
					Input(13, DataType2)
					If UCase(DataType2) = "WAITFOR" Then 'Look to see if the line begins with WAITFOR
						Input(13, TimeToWait)
						Call PauseDelay123(TimeToWait * 1000) 'Pause the routine
					End If
					If UCase(DataType2) = "KEYSTROKE" Then 'Look to see if the line begins with KEYSTROKE
						Input(13, KeyAscii)
						keybd_event(KeyAscii, 0, 0, 0) ' press the character
						keybd_event(KeyAscii, 0, KEYEVENTF_KEYUP, 0) ' release the character
					End If
				Loop 
				FileClose(13)
			Else
				Debug.Print("Shutter Macro could not find the target program") 'A null value has been returned, so alert the user that the window cannot be found
			End If
		Else
			Call MsgBox("I cannot find the macro file to control the camera shutter." & Chr(13) & "Use the settings window to disable the Camera Macro" & Chr(13) & "or locate a valid Camera Macro file", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
		End If
		
LeaveEarly: 
		
		
		TargetHandle = FindWindow(vbNullString, "The Helium Frog Animator V" & Version & " - By Martin Price") 'Find the handle to Helium Frog program
		If (TargetHandle <> 0) Then
			TargetHandle = SetForegroundWindow(TargetHandle) 'Focus back on Helium Frog
		Else
			Debug.Print("Cant refocus on Helium Frog")
		End If
	End Sub
End Module